/******************************************************************************
* This file is part of is32p218 platform,
* Copyright (c) 2009 HangZhou InfoStrong CO.,LTD. All rights reserved.
* This software may only be used under the terms of a valid, current,
* end user license from InfoStrong.
* Nothing else gives you the right to use this software.   
*
* Name:   os_flg.h
* Desc:   OS Flag header file
* Author: Jerry
* Date:   2009-10-19
* Note:   
* History:
*
******************************************************************************/
#include "isOS.h"
#include "os_cfg.h"

#ifndef __OS_MTX_H__
#define __OS_MTX_H__

#if (OS_MTX_EN ==1 )

#ifdef __cplusplus
extern "C" {
#endif
/*-----------------------------------------------------------
 * MACROS AND DEFINITIONS
 *----------------------------------------------------------*/


typedef struct _isOS_MTX
{
   LIST_QUE wait_queue;   //-- List of tasks that wait a mutex
   LIST_QUE mutex_queue;  //-- To include in task's locked mutexes list (if any)
   LIST_QUE lock_mutex_queue; //-- To include in system's locked mutexes list
   isOS_S32 attr;                //-- Mutex creation attr - CEILING or INHERIT

   isOS_TCB * holder;         //-- Current mutex owner(task that locked mutex)
   isOS_S32 ceil_priority;       //-- When mutex created with CEILING attr
   isOS_S32 cnt;                 //-- Reserved
   isOS_S32 id_mutex;            //-- ID for verification(is it a mutex or another object?)
                              // All mutexes have the same id_mutex magic number (ver 2.x)
}isOS_MTX;

#define  OS_MTX_ATTR_CEILING          1
#define  OS_MTX_ATTR_INHERIT          2



isOS_S32 isOS_MtxCreate(isOS_MTX * mutex,
                    isOS_S32 attribute,
                    isOS_S32 ceil_priority);
isOS_S32 isOS_MtxDelete(isOS_MTX * mutex);
isOS_S32 isOS_MtxLock(isOS_MTX * mutex,isOS_U32 timeout);
isOS_S32 isOS_MtxLockPolling(isOS_MTX * mutex);
isOS_S32 isOS_MtxUnlock(isOS_MTX * mutex);
   //-- Routines
isOS_S32 find_max_blocked_priority(isOS_MTX * mutex,isOS_S32 ref_priority);
isOS_S32 try_lock_mutex(isOS_TCB * task);
void remove_task_from_blocked_list(isOS_TCB * task);
isOS_BOOL enable_lock_mutex(isOS_TCB * curr_task,isOS_TCB ** blk_task);
isOS_S32 do_unlock_mutex(isOS_MTX * mutex);


#ifdef __cplusplus
}
#endif

#endif /* #if (OS_MTX_EN ==1 ) End*/
#endif /* __OS_MTX_H__ End */



